;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Question part;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defrule height_get ""
    (logical (start))
   =>
   (assert (UI-state (display height_get)
                     (relation-asserted source_height)
                     (answer-type text))))

(defrule weight_get ""
    (logical (source_height ?h))
   =>
   (assert (UI-state (display weight_get)
                     (relation-asserted source_weight)
                     (answer-type text))))
   
(defrule favtype_female1 ""
    (logical (target_gender f))
    (logical (source_weight ?w))
=>    
    (assert (UI-state (display favtype_female1)
                     (relation-asserted target_type)
                     (response cute)
                     (valid-answers cute sexy gentle neutral))))

   
(defrule favtype_male1 ""
    (logical (target_gender m))
    (logical (source_weight ?w))
=>    
    (assert (UI-state (display favtype_male1)
                     (relation-asserted target_type)
                     (response mature)
                     (valid-answers mature sunny scholar manly))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;End of question part;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;Reasoning part;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defrule gender_filter2
	(declare (salience 11))
	(target_gender f)
	?f1 <- (target_hard_fact (name ?n) (gender m))
=> 	
	(retract ?f1)
)

(defrule gender_filter3
	(declare (salience 11))
	(target_gender m)
	?f1 <- (target_hard_fact (name ?n) (gender f))
=> 	
	(retract ?f1)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;end of hard constrant--gender;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;after hard constrant, build up 4 category cfs(with 0.5).;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defrule decrease_facts4
	(declare (salience 9))
	(target_gender ?g)
	(target_hard_fact (name ?name1) (gender ?gender) )
=>	
	(assert (person_CFs (name ?name1) (appearance-cf 0.5) (edujob-cf 0.5) (character-cf 0.5) (horoscope-cf 0.5)))
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;end of build target person_CFs;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;Following is the part for Appearance category only;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;After Hard Constrant, filter our non-necessary facts from Apperance Initial facts
(defrule decrease_facts_a
	(declare (salience 9))
	(target_gender ?g)
	(target_hard_fact (name ?name1) (gender ?gender) )
	(target_a_current_1 (name ?name2&:(eq ?name1 ?name2)) (body ?body) (T1 ?t1)(T2 ?t2)(T3 ?t3)(T4 ?t4)(travel ?t) (sports ?s) (reading ?r) (kids ?k))
=>	
	(assert (target_a_current (name ?name2) (body ?body) (T1 ?t1)(T2 ?t2)(T3 ?t3)(T4 ?t4)(travel ?t) (sports ?s) (reading ?r) (kids ?k) ))
	(assert (current_goal (category appearance) (name ?name2) (cf 0.5)))
)


;;;;;delete all initial appearance facts(not required anymore)
(defrule delete_initial_facts
	(declare (salience 8))
	(target_gender ?g)
	?f1 <-(target_a_current_1 (name ?name) (body ?body) (T1 ?t1)(T2 ?t2)(T3 ?t3)(T4 ?t4)(travel ?t) (sports ?s) (reading ?r) (kids ?k))
=>
	(retract ?f1)
)
	

(defrule body_get
	(declare (salience 9))
	(source_height ?h)
	(source_weight ?w)
=>	
	(assert (source_bmi (/ ?w (* ?h ?h))))
)

;;;;;;;; body type cf modification;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defrule body_type_male
	(declare (salience 9))
     (target_gender f)
	(source_bmi ?s_bmi)
=>	
    (if (<= ?s_bmi 19) then
        (assert (source_body thin))
	)	
     ( if(>= ?s_bmi 26) then
        (assert (source_body fat))
)
     ( if(and  (> ?s_bmi 19) (< ?s_bmi 26)) then
        (assert (source_body normal))
)
)

(defrule body_type_female
    (declare (salience 9))
     (target_gender m)
    (source_bmi ?s_bmi)
=>
    (if (<= ?s_bmi 18) then
        (assert (source_body thin))
    )
     ( if(>= ?s_bmi 23) then
        (assert (source_body fat))
     )     
     ( if(and  (> ?s_bmi 18) (< ?s_bmi 23)) then
        (assert (source_body normal))
    )   
)

(defrule body_filter_female
    (declare (salience 9))
        (target_gender f)
	(source_body ?s_body)
	(target_a_current (name ?name) (body ?t_body) (T1 ?t1)(T2 ?t2)(T3 ?t3)(T4 ?t4)(travel ?t) (sports ?s) (reading ?r) (kids ?k))
=>	
	(if (eq ?s_body ?t_body) then
	(assert (working_goal (category appearance) (name ?name) (cf 0.5)))
    (assert (reason (name ?name) (category appearance) (subcategory "Body")(reasondesc "Your bodies are of similar size"))))
	(if (and (eq ?s_body thin) (eq ?t_body fat)) then
	(assert (working_goal (category appearance) (name ?name) (cf -0.8))))
	(if (and (eq ?s_body thin) (eq ?t_body normal)) then
	(assert (working_goal (category appearance) (name ?name) (cf  -0.5))))
	(if (and (eq ?s_body normal) (eq ?t_body fat)) then
	(assert (working_goal (category appearance) (name ?name) (cf  -0.5))))
	(if (and (eq ?s_body normal) (eq ?t_body thin)) then
	(assert (working_goal (category appearance) (name ?name) (cf  0.8)))
    (assert (reason (name ?name) (category appearance) (subcategory "Body")(reasondesc "You are fat and your match is thin."))))
	(if (and (eq ?s_body fat) (eq ?t_body thin)) then
	(assert (working_goal (category appearance) (name ?name) (cf  -0.2))))
)

(defrule body_filter_male
    (declare (salience 9))
        (target_gender m)
	(source_body ?s_body)
	(target_a_current (name ?name) (body ?t_body) (T1 ?t1)(T2 ?t2)(T3 ?t3)(T4 ?t4)(travel ?t) (sports ?s) (reading ?r) (kids ?k))
=>	
	(if (eq ?s_body ?t_body) then
	(assert (working_goal (category appearance) (name ?name) (cf  0.5)))
    (assert (reason (name ?name) (category appearance) (subcategory "Body")(reasondesc "Your bodies are of similar size"))))
	(if (and (eq ?s_body fat) (eq ?t_body thin)) then
	(assert (working_goal (category appearance) (name ?name) (cf  -0.8))))
	(if (and (eq ?s_body normal) (eq ?t_body thin)) then
	(assert (working_goal (category appearance) (name ?name) (cf  -0.5))))
	(if (and (eq ?s_body fat) (eq ?t_body normal)) then
	(assert (working_goal (category appearance) (name ?name) (cf  -0.5))))
	(if (and (eq ?s_body thin) (eq ?t_body normal)) then
	(assert (working_goal (category appearance) (name ?name) (cf  0.8)))
    (assert (reason (name ?name) (category appearance) (subcategory "Body")(reasondesc "You are thin and your match is normal"))))
	(if (and (eq ?s_body thin) (eq ?t_body fat)) then
	(assert (working_goal (category appearance) (name ?name) (cf  -0.2))))
)

;;;;;;;;;;;;;;;;;;questions to get user's favourite type;;;;;;;;;;;;;;;;;

(defrule favtype_female2
	(declare (salience 8))
		(target_gender f)
		(target_a_current (name ?name) (body ?t_body) (T1 ?t1)(T2 ?t2)(T3 ?t3)(T4 ?t4)(travel ?t) (sports ?s) (reading ?r) (kids ?k))
		(target_type ?tp)
=>
	(switch ?tp
		(case cute then (assert (working_goal (category appearance) (name ?name) (cf ?t1))))
		(case sexy then (assert (working_goal (category appearance) (name ?name) (cf ?t2))))
		(case gentle then (assert (working_goal (category appearance) (name ?name) (cf ?t3))))
		(case neutral then (assert (working_goal (category appearance) (name ?name) (cf ?t4 ))))
		)
    (if (or (>= 0.5 ?t1) (>= 0.5 ?t2) (>= 0.5 ?t3) (>= 0.5 ?t4)) then 
        (assert (reason (name ?name) (category appearance) (subcategory "Favourite type")(reasondesc "Your match is your favourite type"))))
)
		


(defrule favtype_male2
	(declare (salience 8))
		(target_gender m)
		(target_a_current (name ?name) (body ?t_body) (T1 ?t1)(T2 ?t2)(T3 ?t3)(T4 ?t4)(travel ?t) (sports ?s) (reading ?r) (kids ?k))
		(target_type ?tp)
=>
	(switch ?tp
		(case mature then (assert (working_goal (category appearance) (name ?name) (cf ?t1))))
		(case sunny then (assert (working_goal (category appearance) (name ?name) (cf ?t2))))
		(case scholar then (assert (working_goal (category appearance) (name ?name) (cf ?t3))))
		(case manly then (assert (working_goal (category appearance) (name ?name) (cf ?t4 ))))
		)
    (if (or (>= 0.5 ?t1) (>= 0.5 ?t2) (>= 0.5 ?t3) (>= 0.5 ?t4)) then 
        (assert (reason (name ?name) (category appearance) (subcategory "Favourite type")(reasondesc "Your match is your favourite type"))))
)


(defrule travel1
    (logical (target_type ?t))

=>    
    (assert (UI-state (display travel1)
                     (relation-asserted like_travel)
                     (response y )    
                     (valid-answers y n))))

(defrule sports1
    (logical (like_travel ?t))
=>
        (assert (UI-state (display sports1)
                     (relation-asserted like_sports)
                     (response y)    
                     (valid-answers y n))))

(defrule reading1
    (logical (like_sports ?s))
=>
        (assert (UI-state (display reading1)
                     (relation-asserted like_reading)
                     (response y)    
                     (valid-answers y n))))

(defrule kid1.1
    (logical (like_reading ?r))
=>
        (assert (UI-state (display kid1.1)
                     (relation-asserted want_kid)
                     (response y)    
                     (valid-answers y n))))  

(defrule kid1.2
    (logical (want_kid y))
=>
        (assert (UI-state (display kid1.2)
                     (relation-asserted want_kid_number)
                     (answer-type text)))) 


;;;;;;;;;;;;;;;;;;questions to get user's lifestyle;;;;;;;;;;;;;;;;;


(defrule travel2
	(declare (salience 7))
	(like_travel y)
	(target_a_current (name ?name) (body ?t_body) (T1 ?t1)(T2 ?t2)(T3 ?t3)(T4 ?t4)(travel y) (sports ?s) (reading ?r) (kids ?k))
=>
	(assert (working_goal (category appearance) (name ?name) (cf 0.2)))
    (assert (reason (name ?name) (category appearance) (subcategory "Travel")(reasondesc "You both like travel")))
    
)


(defrule sports2
	(declare (salience 7))
	(like_sports y)
	(target_a_current (name ?name) (body ?t_body) (T1 ?t1)(T2 ?t2)(T3 ?t3)(T4 ?t4)(travel ?t) (sports y) (reading ?r) (kids ?k))
=>
	(assert (working_goal (category appearance) (name ?name) (cf 0.2)))
    (assert (reason (name ?name) (category appearance) (subcategory "Sport")(reasondesc "You both like sports")))
)




(defrule reading2
	(declare (salience 7))
	(like_reading y)
	(target_a_current (name ?name) (body ?t_body) (T1 ?t1)(T2 ?t2)(T3 ?t3)(T4 ?t4)(travel ?t) (sports ?s) (reading y) (kids ?k))
=>
	(assert (working_goal (category appearance) (name ?name) (cf 0.2)))
    (assert (reason (name ?name) (category appearance) (subcategory "Reading")(reasondesc "You both like reading")))
)

(defrule kid2
	(declare (salience 7))
	(want_kid n)
	(target_a_current (name ?name) (body ?t_body) (T1 ?t1)(T2 ?t2)(T3 ?t3)(T4 ?t4)(travel ?t) (sports ?s) (reading ?r) (kids ?k))
=>
	(if (eq ?k 0) then
	(assert (working_goal (category appearance) (name ?name) (cf 0.5)))
    (assert (reason (name ?name) (category appearance) (subcategory "Kid")(reasondesc "Neither of you want to have kids")))
	else
	(assert (working_goal (category appearance) (name ?name) (cf -0.8))))
)

(defrule kid3
	(declare (salience 7))
	(want_kid y)
	(target_a_current (name ?name) (body ?t_body) (T1 ?t1)(T2 ?t2)(T3 ?t3)(T4 ?t4)(travel ?t) (sports ?s) (reading ?r) (kids 0))
=>
	(assert (working_goal (category appearance) (name ?name) (cf -0.8)))
)

(defrule kid4
	(declare (salience 7))
	(want_kid y)
	(want_kid_number ?number)
	(target_a_current (name ?name) (body ?t_body) (T1 ?t1)(T2 ?t2)(T3 ?t3)(T4 ?t4)(travel ?t) (sports ?s) (reading ?r) (kids ?k&:(neq ?k 0)))
=>
	(if (and (> (- ?number ?k) -2) (< (- ?number ?k) 2)) then
	(assert (working_goal (category appearance) (name ?name) (cf 0.3))))
     (assert (reason (name ?name) (category appearance) (subcategory "Kid")(reasondesc "You both want to have kids")))
)





;;;;;;;;;;;;;Transfer the appearance cf to the total 4 category cf facts.
(defrule appearenceto4
	;(declare (salience -9))
	(current_goal (category appearance) (name ?name1) (cf ?cf))
	?f1 <- (person_CFs (name ?name2&:(eq ?name1 ?name2)) (appearance-cf ?a-cf&:(neq ?a-cf ?cf)) (edujob-cf ?e-cf) (character-cf ?c-cf) (horoscope-cf ?h-cf))
=>
	(assert (person_CFs (name ?name2) (appearance-cf ?cf) (edujob-cf ?e-cf) (character-cf ?c-cf) (horoscope-cf ?h-cf)))
	(retract ?f1)    
    (assert (begin_background))

)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;End of Appearance category;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;====================================END OF APPEARANCE=================================
